package com.sluggard.mp.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

import java.time.LocalDateTime;

import org.apache.ibatis.reflection.MetaObject;

/**
 * @author lizheng
 * @since 2024.4.17
 */
public class CustomMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasSetter("version")) {
            this.strictInsertFill(metaObject, "version", () -> 0L, Long.class);
        }
        if (metaObject.hasSetter("deleted")) {
            this.strictInsertFill(metaObject, "deleted", () -> Boolean.FALSE, Boolean.class);
        }
        if (metaObject.hasSetter("createTime")) {
            this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        }
        if (metaObject.hasSetter("updateTime")) {
            this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("updateTime")) {
            this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        }
        if (metaObject.hasSetter("modifyTime")) {
            this.strictUpdateFill(metaObject, "modifyTime", LocalDateTime::now, LocalDateTime.class);
        }
    }
}
